package com.xpalive.utils.qr;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.Binarizer;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.EncodeHintType;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.Result;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.xpalive.utils.qr.QRConstants.FormatName;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import javax.imageio.ImageIO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;

/**
 * Created by shenyuyang on 2016/11/24.
 */
@Slf4j
public class QRHelper {

    public static String absPicPath(String targetPath, String targetName) {
        if (!targetPath.endsWith(File.separator)) {
            targetPath = targetPath + File.separator;
        }
        return targetPath + targetName;
    }

    public static File makeCode(String content, File logoFile, String targetPath, String targetName,
        FormatName format, int side) throws Exception {
        try {
            File dir = new File(targetPath);
            if (dir.exists() == false) {
                dir.mkdirs();
                log.info("mkdir path ok");
            }
            File file = new File(absPicPath(targetPath, targetName));
            byte[] bytes = makeCode(content, logoFile, format, side, side, 2);
            FileUtils.writeByteArrayToFile(file, bytes);
            return file;
        } catch (Exception e) {
            log.error("make qrcode error", e);
            throw e;
        }
    }

    public static byte[] makeCode(String content, File logoFile, FormatName format, int width,
        int height,
        int margin) throws Exception {
        try {
            MultiFormatWriter multiFormatWriter = new MultiFormatWriter();

            Map hints = new HashMap();
            hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");//图片编码
            hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);//图片编码
            hints.put(EncodeHintType.MARGIN, margin);//图片边距
            BitMatrix bitMatrix = multiFormatWriter
                .encode(content, BarcodeFormat.QR_CODE, width, height, hints);
            return MatrixToImageWriter.writeToByte(bitMatrix, format, logoFile);
        } catch (Exception e) {
            log.error("make qrcode error", e);
            throw e;
        }
    }

    public static String readCode(String picAbsPath) throws Exception {

        try {
            MultiFormatReader formatReader = new MultiFormatReader();
            File file = new File(picAbsPath);
            BufferedImage image = ImageIO.read(file);

            LuminanceSource source = new BufferedImageLuminanceSource(image);
            Binarizer binarizer = new HybridBinarizer(source);
            BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
            Map hints = new HashMap();
            hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
            Result result = formatReader.decode(binaryBitmap, hints);

            log.info("result = " + result.toString());
            log.info("resultFormat = " + result.getBarcodeFormat());
            log.info("resultText = " + result.getText());

            return result.getText();

        } catch (Exception e) {
            log.info("read qrcode error", e);
            throw e;
        }

    }

    public static void main(String[] agrs) throws Exception {
        makeCode(
            "http://hzs.ksfj.org/huazangsi_api/wx-user/getOAuthUrl?requestUrl=religiousSpec7e4e0ece-6fe7-4058-818f-ce7c9ba529bb",
            new File("C:\\Users\\xiongping\\Desktop\\resource\\图片\\ddvp.jpg"),
            "C:\\Users\\xiongping\\Desktop\\resource\\图片", "abc.png", FormatName.png, 256);
    }


}
